package com.zhongyewx.teachercert.views;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

import com.zhongyewx.teachercert.utils.MeasureUtils;
import com.zhongyewx.teachercert.utils.ZYDensityUtil;

/**
 * @author h.f
 * @version 2019/7/24 二阶贝塞尔曲线
 */
public class DrawSecondBesselView extends View {

    private int mWidth;
    private int mDefaultHeight;
    private int mHeight;
    private int mWaveHeight;
    private int mWaveDx;
    private int dx;
    private Paint mPaint;
    private Paint mPaint2;
    private int dxIndex = 3;
    private ValueAnimator valueAnimator;

    public DrawSecondBesselView(Context context) {
        super(context);
        init();
    }

    public DrawSecondBesselView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public DrawSecondBesselView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPaint2 = new Paint();
        mPaint2.setAntiAlias(true);
        mPaint2.setDither(true);
        mPaint2.setColor(Color.parseColor("#6620B2AA"));
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setColor(Color.parseColor("#9920B2AA"));
        //波长的长度(这里设置为屏幕的宽度)
        mWaveDx = getResources().getDisplayMetrics().widthPixels;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //控件的宽高
        mWidth = MeasureUtils.measureView(widthMeasureSpec, mWaveDx);
        mDefaultHeight = MeasureUtils.measureView(heightMeasureSpec, 300);
        if (mHeight == 0) {
            mHeight = mDefaultHeight;
        }
        //水波的高度
        mWaveHeight = ZYDensityUtil.dip2px(getContext(), 20);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Path path = new Path();
        Path path1 = new Path();
        path.reset();
        path.moveTo(-mWaveDx + dx, mHeight / 2);
        path1.reset();
        path1.moveTo(-mWaveDx + dx, mHeight / 2);
        for (int i = -mWaveDx; i < getWidth() + mWaveDx; i += mWaveDx) {
            path1.rQuadTo(mWaveDx / 4, -mWaveHeight, mWaveDx / 2, 0);
            path1.rQuadTo(mWaveDx / 4, -mWaveHeight, mWaveDx / 2, 0);
            path.rQuadTo(mWaveDx / 4, -mWaveHeight, mWaveDx / 2, 0);
            path.rQuadTo(mWaveDx / 4, mWaveHeight, mWaveDx / 2, 0);
        }
        if (mHeight >= mDefaultHeight*2){
            dxIndex = -Math.abs(dxIndex);
        } else if (mHeight <= mDefaultHeight){
            dxIndex = Math.abs(dxIndex);
        }

        mHeight = mHeight + dxIndex;
            //绘制封闭的区域
        path1.lineTo(mWidth, mHeight);
        path1.lineTo(0, mHeight);

        //绘制封闭的区域
        path.lineTo(mWidth, mHeight);
        path.lineTo(0, mHeight);

        path1.close(); //绘制封闭的区域
        path.close();
        canvas.drawPath(path, mPaint);
        canvas.drawPath(path1, mPaint2);
    }

    public void startAnimation() {
        valueAnimator = ValueAnimator.ofInt(0, mWaveDx);
        valueAnimator.setDuration(2000);
        valueAnimator.setRepeatCount(ValueAnimator.INFINITE);
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                //水平方向的偏移量
                dx = (int) animation.getAnimatedValue();
                postInvalidate();
            }

        });
        valueAnimator.start();

    }

    public void release(){
        if (valueAnimator != null) {
            valueAnimator.cancel();
            valueAnimator.removeAllUpdateListeners();
        }
    }
}